<template>
  <Dialog title="充值申请" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="130px"
      v-loading="formLoading"
    >
      <el-form-item label="目标客户" prop="targetTenantId">
        <el-select
          class="w-100%"
          v-model="formData.targetTenantId"
          placeholder="请选择要充值的客户"
        >
          <el-option
            v-for="item in customerOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="充值金额" prop="rechargeAmount">
        <el-input type="number" v-model="formData.rechargeAmount" placeholder="请输入充值金额" />
      </el-form-item>
      <el-form-item label="充值类型" prop="rechargeType">
        <el-select class="w-100%" v-model="formData.rechargeType" placeholder="请选择充值类型">
          <el-option label="点呼话费" :value="1"> </el-option>
          <el-option label="群呼话费" :value="2"> </el-option>
          <el-option label="坐席" :value="3"> </el-option>
          <el-option label="企业领取上限扩容" :value="4"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="充值数量">
        <el-input type="number" v-model="formData.rechargeNum" placeholder="请输入充值数量" />
      </el-form-item>

      <el-form-item label="附件" prop="attachment">
        <UploadImg v-model="formData.attachment" :limit="1" :is-show-tip="false"> </UploadImg>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as proxyApi from '@/api/home/proxy.ts'

defineOptions({ name: 'Apeningapplication' })

const { t } = useI18n()
const message = useMessage()

const dialogVisible = ref(false)
const formLoading = ref(false)

const formData = ref({
  rechargeAmount: undefined,
  rechargeType: undefined,
  rechargeNum: undefined,
  attachment: undefined,
  remark: undefined,
  targetTenantId: undefined
})
const formRules = reactive({
  rechargeAmount: [{ required: true, message: '充值金额不能为空', trigger: 'blur' }],
  rechargeType: [{ required: true, message: '充值类型不能为空', trigger: 'change' }],
  targetTenantId: [{ required: true, message: '操作租户对象不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  getMyCustomer()
  resetForm()
}
defineExpose({ open })

/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    await proxyApi.createRechargeList(formData.value)
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
  } finally {
    formLoading.value = false
  }
}
const customerOptions = ref([])
/**客户列表*/
const getMyCustomer = async () => {
  customerOptions.value = await proxyApi.getProxyCustomer()
}
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    rechargeAmount: undefined,
    rechargeType: undefined,
    contactNum: undefined,
    attachment: undefined,
    remark: undefined,
    targetTenantId: undefined
  }
  formRef.value?.resetFields()
}
</script>
